<template>
  <div class="header">
    <el-menu class="nav hidden-md-and-down" :default-active="activeIndex" mode="horizontal">
      <h1 class="logo" @click="goto('/')" alt="曾伟浩,hen,再往后">hen</h1>
      <el-menu-item index="/" @click="goto('/')">首页</el-menu-item>
      <el-submenu index="/tag">
        <template slot="title">标签</template>
        <!-- <el-menu-item index="/tag/1" @click="goto('/tag/1')">作品集<i class="el-icon-star-on"></i></el-menu-item> -->
        <el-menu-item v-for="(item,index) in tagList" :key="index" :index="'/tag/' + item.id"
          @click="goto('/tag/' + item.id)">{{item.name}}<i v-if="item.mark" class="el-icon-star-on"></i></el-menu-item>
        <el-menu-item v-if="tagList.length <= 0">暂无标签</el-menu-item>
      </el-submenu>
      <el-menu-item index="/timeline" @click="goto('/timeline')">时间轴</el-menu-item>
      <el-menu-item index="/message" @click="goto('/message')">留言</el-menu-item>
      <el-menu-item index="/about" @click="goto('/about')">关于</el-menu-item>
      <el-menu-item index="/user" @click="goto('/user')" class="nav_right_item"><i class="el-icon-user-solid"></i>
      </el-menu-item>
      <!-- <div class="nav_right_item" @click="goto('/user')"><i class="el-icon-user-solid"></i></div> -->
      <div class="nav_right_item" @click="changeTheme"><i :class="theme?'el-icon-sunny':'el-icon-moon'"></i></div>
      <div class="nav_right_item" @click="dialogVisible = true"><i class="el-icon-search"></i></div>
    </el-menu>

    <!-- 移动端导航 -->
    <div class="nav hidden-lg-and-up">
      <h1 class="logo" @click="goto('/')">hen</h1>
      <div class="nav_right_item nav_menu" @click="drawer = true"><i class="el-icon-menu"></i></div>
    </div>
    <el-drawer title="" size="70%" :visible.sync="drawer" :modal-append-to-body="false" direction="rtl">
      <el-menu :default-active="activeIndex">
        <el-menu-item index="/" @click="goto('/')">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-submenu index="/tag">
          <template slot="title">
            <i class="el-icon-s-management"></i>
            <span>标签</span>
          </template>
          <el-menu-item-group>
            <el-menu-item v-for="(item,index) in tagList" :key="index" :index="'/tag/' + item.id"
              @click="goto('/tag/' + item.id)">{{item.name}}<i v-if="item.mark" class="el-icon-star-on"></i>
            </el-menu-item>
          </el-menu-item-group>
        </el-submenu>
        <el-menu-item index="/timeline" @click="goto('/timeline')">
          <i class="el-icon-s-flag"></i>
          <span slot="title">时间轴</span>
        </el-menu-item>
        <el-menu-item index="/message" @click="goto('/message')">
          <i class="el-icon-s-comment"></i>
          <span slot="title">留言</span>
        </el-menu-item>
        <el-menu-item index="/about" @click="goto('/about')">
          <i class="el-icon-info"></i>
          <span slot="title">关于</span>
        </el-menu-item>
        <el-menu-item index="/user" @click="goto('/user')">
          <i class="el-icon-s-custom"></i>
          <span slot="title">个人中心</span>
        </el-menu-item>
        <div class="mobile_tool">
          <i @click="dialogVisible = true" class="el-icon-search"></i>
          <i @click="changeTheme" :class="theme?'el-icon-sunny':'el-icon-moon'"></i>
          <i @click="isLogin = true" class="el-icon-monitor"></i>
        </div>
      </el-menu>
    </el-drawer>

    <el-dialog :visible.sync="dialogVisible" width="90%" :show-close="false" :modal-append-to-body="false">
      <span slot="title">
        <el-input placeholder="请输入内容" @input="searchArticles(searchKey)" prefix-icon="el-icon-search"
          v-model="searchKey" clearable>
        </el-input>
      </span>
      <el-table :data="list" @row-click="toSelect">
        <el-table-column property="title" label="文章"></el-table-column>
        <el-table-column property="description" label="描述"></el-table-column>
        <el-table-column label="标签">
          <template slot-scope="scope">
            <el-tag style="margin-right:4px;" v-for="(item,index) in scope.row.tags" :key="index" type="success">
              {{item.name}}</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'Header',
  data() {
    return {
      isLogin: false,
      drawer: false, // 移动导航栏显示
      theme: false,
      activeIndex: '/',
      dialogVisible: false,
      searchKey: '',
      tagList: [],
      list: [],
    }
  },
  mounted() {
    //  initTheme(this.theme)
    this.getTagList()
  },
  watch: {
    $route(to, from) {
      this.activeIndex = to.path // 动态高亮导航 防止刷新不能正确指向当前路由
      // console.log( this.activeIndex)
    },
  },
  methods: {
    // 搜索跳转
    toSelect(row, column, event){
      this.$router.push('/article/' + row.id)
      this.dialogVisible = false
    },
    // 文章搜索
    searchArticles(key) {
      this.$api.httpArticlSearch(key).then((res) => {
        if (res.code == 200) {
          this.list = res.data.rows || []
        }
      })
    },
    // 获取标签
    getTagList() {
      this.$api.httpTagList().then((res) => {
        if (res.code == 200) {
          this.tagList = res.data || []
        }
      })
    },
    // 改变主题色
    changeTheme() {
      this.theme = !this.theme
      this.$emit('changeTheme')
    },
    goto(path) {
      this.$router.push(path)
    },
  },
}
</script>

<style scoped lang="scss">

.header {
  position: sticky;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 2000;
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;

  // 移动导航栏
  .mobile_tool {
    font-size: 14px;
    padding: 0 20px;
    padding-left: 20px;
    box-sizing: border-box;
    line-height: 56px;
    color: #909399;

    i {
      margin-right: 14px;
      width: 24px;
      text-align: center;
      font-size: 18px;
      vertical-align: middle;
      cursor: pointer;
    }
  }
  // 导航栏
  .nav {
    width: 1200px;
    margin: auto;
    border-bottom: none; // 取消默认边线
    overflow: hidden;

    .logo {
      padding: 0;
      margin: 0;
      float: left;
      height: 60px;
      line-height: 60px;
      color: #909399;
      margin: 0 60px 0 0;
      display: block;
      cursor: pointer;
    }
    .nav_right_item {
      float: right;
      height: 60px;
      line-height: 60px;
      padding: 0 10px;
      color: #909399;
      cursor: pointer;
    }
    // 移动端菜单
    .nav_menu{
      font-size: 22px;
    }
  }
}
@media screen and (max-width: 1200px) {
  .header .nav {
    width: 100%;
  }
}
</style>
